const DB = require('../init');
const {Sequelize,Op} = require('sequelize');
const fw = require('../../core/framework');
const moment = require('moment');

module.exports = {
    async create(data) {
        DB.AlarmEvent.sync();
        if(data.start_time){
            data.start_time = moment(data.start_time).add(-8,'hour').format('YYYY-MM-DD HH:mm:ss')
        }
        if(data.end_time){
            data.end_time = moment(data.end_time).add(-8,'hour').format('YYYY-MM-DD HH:mm:ss')
        }
        return await DB.AlarmEvent.create(data);
    },
    async delete(data) {
        return await DB.AlarmEvent.destroy({
            where: {
                id: data.id
            }
        });
    },
    async update(data) {
        if(data.start_time){
            data.start_time = moment(data.start_time).add(-8,'hour').format('YYYY-MM-DD HH:mm:ss')
        }
        if(data.end_time){
            data.end_time = moment(data.end_time).add(-8,'hour').format('YYYY-MM-DD HH:mm:ss')
        }
        return await DB.AlarmEvent.update(data, {
            where: {
                id: data.id
            }
        });
    },
    async search(data) {
        return await DB.AlarmEvent.findAll({
            where: data
        });
    },
    async getLastEvent(data){
        return await DB.AlarmEvent.findAll({
            where: data,
            order: [
                ['created_at', 'desc'] // 按时间倒序，最新创建的在最上面。  正序是asc
            ]
        });
    },
    async untreatedSearch(data){
        return await DB.AlarmEvent.findAll({
            where: data,
            order: [
                ['start_time', 'desc'] // 按时间倒序，最新发生的在最上面。  正序是asc
            ]
        });
    },
    async searchAll(data){
        let currentPage = data.index || 0;
        let count = data.size || 10;
        let offset = currentPage * count;
        let where = {
            merchant_id:data.merchant_id
        };
        if(data.date){
            let date = moment(data.date).format('YYYY-MM-DD');
            where['start_time'] = {
                [Op.lt]: new Date(date+' 23:59:59'),
                [Op.gt]: new Date(date+' 00:00:00')
            }
        }
        if(data.handle_type){
            where['handle_type'] = data.handle_type;
        }
        let query = {
            offset:offset,
            limit: parseInt(count),
            where:where,
            order: [
                ['start_time', 'desc'] // 按时间倒序，最新创建的在最上面。  正序是asc
            ],
            attributes: {
                exclude: ['createdAt', 'updatedAt','password'],
                include: [
                    [Sequelize.fn('date_format', Sequelize.col('start_time'), '%Y-%m-%d %H:%i:%s'), 'start_time'],
                    [Sequelize.fn('date_format', Sequelize.col('end_time'), '%Y-%m-%d %H:%i:%s'), 'end_time']
                ]
            },
            include: [{
                model: DB.Device,
                as: 'device',
                attributes: ['name']
            },{
                model: DB.Face,
                as: 'face',
                attributes: ['name']
            }]
        };
        if(data.device_id){
            query.where.device_id = data.device_id
        }
        return await DB.AlarmEvent.findAndCountAll(query).then(res => {
            return fw.genPageData(data,res);
        });
    }
};
